package yue.shen.android.demo.view.banner

import android.view.View
import android.view.ViewGroup
import androidx.viewpager.widget.PagerAdapter

class AutoLoopBannerAdapter(private val list: ArrayList<View>) : PagerAdapter() {

    override fun getCount(): Int {
        return list.size
    }

    override fun isViewFromObject(view: View, any: Any): Boolean {
        return view === any
    }

    override fun destroyItem(container: ViewGroup, position: Int, any: Any) {
        container.removeView(any as View)
    }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val view = list[position]
        if (view.parent == null) {
            container.addView(view)
        }
        return view
    }

    fun updateData(list: ArrayList<View>) {
        this.list.clear()
        this.list.addAll(list)
        notifyDataSetChanged()
    }
}